home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / easyproc2.lha / EasyProcess / launch / StartProc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  3.4 KB  |  151 lines

  1. #ifdef KICK_13
  2. #include <libraries/arpbase.h>
  3. #include <clib/arp_protos.h>
  4. #else
  5. extern struct Library *DOSBase;
  6. #endif
  7. #include "Launch.h"
  8. #include "LaunchPriv.h"
  9.  
  10. struct List *ProcPairList;
  11.  
  12. /*
  13.  *    NAME
  14.  *        StartProcess -- start a new process.
  15.  *
  16.  *    SYNOPSIS
  17.  *        Child = StartProcess (Name, Entry, Data, Priority)
  18.  *
  19.  *        struct Process *StartProcess (char *, void *, void *, WORD);
  20.  *
  21.  *    FUNCTION
  22.  *        Create a process to start at the entry point specified and
  23.  *        pass it the data.
  24.  *
  25.  *    DESCRIPTION
  26.  *        We check if the process pair list already exists, if not we
  27.  *        create it.  We then allocate a ProcPair structure to hold all
  28.  *        the data needed for the control:
  29.  *
  30.  *            Both processes' structure pointer, the signal bits,
  31.  *            the entry point and data for the child.
  32.  *
  33.  *        We then try to start the child.  If successful, we add the
  34.  *        process pair to the list.
  35.  *
  36.  *        In order not to have too many signal bit allocated, we always
  37.  *        check if the parent has already launched another child which
  38.  *        is still active and use the same signal bit.
  39.  *
  40.  *    INPUT
  41.  *        Name - the name for the new process, does NOT need to be unique.
  42.  *        Entry - the entry point (function) for the enw process.
  43.  *        Data - the data (anything) to be passed as an argument to the
  44.  *            entry point of the new process.
  45.  *
  46.  *    OUTPUT
  47.  *        Proc - the new process' structure pointer.
  48.  *
  49.  *    NOTE
  50.  *        We use arp.library, but it can be adapted easily to 2.04.
  51.  *
  52.  *    HISTORY
  53.  *        1992/09/06    Pierre Baillargeon        Creation
  54.  *        1992/10/25    Pierre Baillargeon        Update
  55.  *                    Adapted to Kickstart 2.0.
  56.  *
  57.  *    SEE ALSO
  58.  *        KillProcess(), KillProcesses(), WaitProcess(), WaitProcesses()
  59.  */
  60.  
  61. struct Process *StartProcess (char *Name, void *EntryPoint, void *Data, WORD Priority)
  62. {
  63.     struct ProcPair *pp = NULL;
  64.     struct Process *p = NULL;
  65. #ifdef KICK_13
  66.     struct ProcessControlBlock    PCB = { 8192L, 0,
  67.                                         PRF_CODE | PRF_NOCLI | PRF_SAVEIO,
  68.                                         0, 0, 0, 0, ChildEntry, 0, 0 };
  69.     PCB.pcb_Pri = Priority;
  70. #else
  71.     struct TagItem Tags[5] = { {NP_Entry, ChildEntry}, { NP_StackSize, 8192 },
  72.         { NP_Name, NULL }, {NP_Priority, 0}, { TAG_DONE, 0 } };
  73.  
  74.     Tags[2].ti_Data = (ULONG)Name;
  75.     Tags[3].ti_Data = (ULONG)Priority;
  76. #endif
  77.  
  78.     Forbid ();
  79.  
  80.     /*
  81.      *    Create the list if it does not exist.
  82.      */
  83.  
  84.     if (NULL == ProcPairList)
  85.     {
  86.         ProcPairList = (struct List *)AllocMem (sizeof (struct List), MEMF_CLEAR);
  87.         if (NULL == ProcPairList)
  88.         {
  89.             goto Error;
  90.         }
  91.         NewList (ProcPairList);
  92.     }
  93.  
  94.     /*
  95.      *    Create and initialize the process pair.
  96.      */
  97.  
  98.     pp = (struct ProcPair *)AllocMem (sizeof (struct ProcPair), MEMF_CLEAR);
  99.     if (NULL == pp)
  100.     {
  101.         goto Error;
  102.     }
  103.  
  104.     pp->pp_Node.ln_Type = 0;
  105.     pp->pp_Node.ln_Pri = 0;
  106.     pp->pp_Parent = (struct Process *)FindTask (NULL);
  107.  
  108.     /*
  109.      *    Try to find if this process has already launched another and
  110.      *    use the same signal bit.
  111.      */
  112.  
  113.     if (-1L == (pp->pp_ParentBit = AllocParentSignal ()))
  114.     {
  115.         goto Error;
  116.     }
  117.  
  118.  
  119.     /*
  120.      *    Launch child, if successful add the process pair to the list.
  121.      */
  122.  
  123.     pp->pp_ChildEntry = (void (*__saveds)(void *))EntryPoint;
  124.     pp->pp_ChildData = Data;
  125.  
  126. #ifdef KICK_13
  127.     if (ASyncRun (Name, NOCMD, &PCB) >= 0L)
  128.     {
  129.         p = (struct Process *)(((char *)PCB.pcb_WBProcess) - sizeof (struct Task));
  130. #else
  131.     if (NULL != (p = CreateNewProc (Tags)))
  132.     {
  133. #endif
  134.         pp->pp_Child = p;
  135.         AddHead (ProcPairList, &pp->pp_Node);
  136.         Signal (p, 1L << p->pr_MsgPort.mp_SigBit);
  137.         Permit ();
  138.         return p;
  139.     }
  140.  
  141. Error:
  142.     if (pp)
  143.     {
  144.         FreeParentSignal (pp->pp_ParentBit);
  145.         FreeMem (pp, sizeof (struct ProcPair));
  146.     }
  147.     FreeProcPairList ();
  148.     Permit ();
  149.     return NULL;
  150. }
  151.